////////////////////////////////////////////////////////////////////////////////
// Copyright 2006 CORE Solutions Limited
// All rights reserved.
// PROPRIETARY AND CONFIDENTIAL INFORMATION.
// Use is subject to license terms.
//
// FUNCTION ID      :   ITEM_CATEGORY
// AUTHOR           :   Alex Cheung 
// DATE CREATE      :   2006/01/20
// DESCRIPTION      :   a ACTION file for the function, ITEM_CATEGORY
//                  :   
//                  :   
//                  :   
//
// MODIFICATION HISTORY:
// DATE         AUTHOR          VERSION NO.         COMMENT
// -----------  --------------  ------------------  -------------------
// 2006/01/20   Alex Cheung     1.0                 source is generated.
//
//
//
////////////////////////////////////////////////////////////////////////////////

package com.erp.app.catalog.itemCategory.web;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.erp.app.common.ProjectConstants;
import com.erp.app.catalog.itemCategory.criteria.ItemCategorySrchCriteria;
import com.erp.app.catalog.itemCategory.pk.ItemCategoryPk;
import com.erp.app.catalog.itemCategory.vo.ItemCategoryVo;
import com.erp.app.catalog.itemLine.pk.ItemLinePk;
import com.erp.app.util.web.WebUtil;
import com.erp.framework.controller.action.SimplePageEditStyleAction;
import com.erp.framework.util.CommonUtil;
import com.erp.framework.util.logger.Logger;
import com.erp.framework.util.user.User;

/**
* A <strong>Page Edit Action</strong> is an adapter between the contents of an incoming
* HTTP request and the corresponding business logic that should be executed to
* process this request.  The controller (ActionServlet) will select an
* appropriate Action for each request.
* The main purpose for this action class is to handle the one-page document events
* from the web browsers.
*
* @author          Alex Cheung 
* @version 1.0     2006/01/20
* @since           FrameWork 1.0
*
*/
public class ItemCategoryAction extends SimplePageEditStyleAction {

	////////////////////////////////////////////////////////////////////////////////
	// CONSTANTS DEFINITION
	////////////////////////////////////////////////////////////////////////////////

	//public static final String ACTION_DO_SOMETHING = "actionDoSomething";
	//public static final String STATUS_DO_SOMETHING = "statusDoSomething";

	public static final String ACTION_EDIT_ITEM_CATEGORY = "editItemCategory";
	
	public static final String ACTION_BACK = "back";
	public static final String LABEL_TO_BE_GEN = "<To be generated.>";

	////////////////////////////////////////////////////////////////////////////////
	// GLOBAL VARIABLES DEFINITION
	////////////////////////////////////////////////////////////////////////////////



	////////////////////////////////////////////////////////////////////////////////
	// LOGICAL AND BUSINESS METHODS
	////////////////////////////////////////////////////////////////////////////////

	
	public ActionForward handlePageEvent(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		HttpSession session = request.getSession(false);
		ItemCategoryForm form = (ItemCategoryForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {
			if (!isHandled(request) && action.equalsIgnoreCase(ACTION_EDIT_ITEM_CATEGORY)) {
				actionForward = processActionEditItemCategory(mapping, formIn, request, response, errors);
				setHandled(request);
				saveToken(request);
			} else if (!isHandled(request) && action.equalsIgnoreCase(ACTION_BACK)) {
				actionForward = mapping.findForward(ACTION_BACK);
			}

		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}

		return actionForward;
	}

	protected ActionForward processOtherStatus(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response,
		ActionErrors errors, String rb_status) {

		HttpSession session = request.getSession(false);
		ItemCategoryForm form = (ItemCategoryForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {
//			if (rb_status.equalsIgnoreCase(STATUS_DO_SOMETHING)) {
//				return processStatusDoSomething(mapping,  formIn,  request, response,  errors,  rb_status);
//			}
		} catch (Exception e) {
			Logger.log(this, e);
		}

		return actionForward;
	}

	//uncomment the following code and implement the method if necessary
	
	protected ActionForward processActionEditItemCategory(ActionMapping mapping, ActionForm formIn, HttpServletRequest request,
            HttpServletResponse response, ActionErrors errors) {

        HttpSession session = request.getSession(false);
        ItemCategoryForm form = (ItemCategoryForm) formIn;
        ActionForward actionForward = new ActionForward(mapping.getInput());
        String action = CommonUtil.nullToString(form.getAction());
        User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);
                
        String editItemCategory = form.getEditItemCategory();
        if (!CommonUtil.isEmpty(editItemCategory)) {
	        String companyCode = WebUtil.getAppropriateCompanyCode(session, null);
	        ItemLinePk editingItemLinePk = (ItemLinePk) session.getAttribute(ProjectConstants.SESSION_EDIT_ITEM_LINE_PK);
	        
	        ItemCategoryPk pk = new ItemCategoryPk(companyCode, editingItemLinePk.getItemLine(), editItemCategory);
	        
	        session.removeAttribute(ProjectConstants.SESSION_EDIT_ITEM_CATEGORY_PK);
	        session.setAttribute(ProjectConstants.SESSION_EDIT_ITEM_CATEGORY_PK, pk);
	        actionForward = mapping.findForward(ACTION_EDIT_ITEM_CATEGORY);
	        
        }
        return actionForward;
    }

	////////////////////////////////////////////////////////////////////////////////
	// END OF LOGICAL AND BUSINESS METHODS
	////////////////////////////////////////////////////////////////////////////////


	////////////////////////////////////////////////////////////////////////////////
	// OVERRIDES PARENT'S OPTIONAL METHODS
	////////////////////////////////////////////////////////////////////////////////

	//the following method is only to be overwritten by sub-class if intended
	//to set the aut-search on-load to on/off
	protected boolean getLoadOnEnter() {

		//auto-search when load
		return true;
	}

	//the following method is only to be overwritten by sub-class if intended
	//to set the clearing search result sets on-load to on/off
	protected boolean getResetOnEnter() {

		//reset last searched results when load 
		return true;
	}

	protected void initAction(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		super.initAction(mapping, formIn, request, response, errors);

		HttpSession session = request.getSession(false);
		ItemCategoryForm form = (ItemCategoryForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {
//			HashMap errMsgMap = WebUtil.getModuleErrorMessages(editingUser.getCompanyCode(),
//				DBMENU_GENERAL_MENU, form.getModuleName(), form.getSysLangCode(), DBMENU_OBJECT_TYPE_ERR_MSG);
//			form.setErrMsgMap(errMsgMap);

		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}
	}

	protected void initForm(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		super.initForm(mapping, formIn, request, response, errors);

		HttpSession session = request.getSession(false);
		ItemCategoryForm form = (ItemCategoryForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {

		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}
	}

	protected void initPage(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		super.initPage(mapping, formIn, request, response, errors);

		HttpSession session = request.getSession(false);
		ItemCategoryForm form = (ItemCategoryForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {

		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}
	}

	protected void beforeSearch(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		HttpSession session = request.getSession(false);
		ItemCategoryForm form = (ItemCategoryForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {

		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}
	}

	protected void afterSearch(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		HttpSession session = request.getSession(false);
		ItemCategoryForm form = (ItemCategoryForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {

		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}
	}

	protected void beforeSave(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		HttpSession session = request.getSession(false);
		ItemCategoryForm form = (ItemCategoryForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {
			/*ArrayList rows = form.getMultiRows();
			for(int i = 0; i< rows.size(); i++){
				ItemCategoryRow row = (ItemCategoryRow) rows.get(i);
				if(LABEL_TO_BE_GEN.equals(row.getCategoryItemCategory())){
					row.setCategoryItemCategory(String.valueOf(-1*i));
				}
			}*/
			
		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}
	}

	protected void afterSave(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		HttpSession session = request.getSession(false);
		ItemCategoryForm form = (ItemCategoryForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {
			ArrayList rows = form.getMultiRows();
			for(int i = 0; i< rows.size(); i++){
				ItemCategoryRow row = (ItemCategoryRow) rows.get(i);
				if(LABEL_TO_BE_GEN.equals(row.getCategoryItemCategory())){
					row.setCategoryItemCategory("-99");
				}
			}
		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}
	}

	protected void beforeRefresh(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		HttpSession session = request.getSession(false);
		ItemCategoryForm form = (ItemCategoryForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {

		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}
	}

	protected void afterRefresh(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		HttpSession session = request.getSession(false);
		ItemCategoryForm form = (ItemCategoryForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {

		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}
	}

	protected void beforeAddRow(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		HttpSession session = request.getSession(false);
		ItemCategoryForm form = (ItemCategoryForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {

		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}
	}


		//TODO: Add the following line to ProjectSecurityConstants.java
		//public static final String SP_SHARE_ITEM_CATEGORY = "SP_SHARE_ITEM_CATEGORY";
	protected void afterAddRow(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		HttpSession session = request.getSession(false);
		ItemCategoryForm form = (ItemCategoryForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {
			ArrayList rows = form.getMultiRows();
			if (rows != null && rows.size() > 0) {
				ItemCategoryRow row = (ItemCategoryRow) rows.get(rows.size() - 1);
				row.setCompanyCode(WebUtil.getAppropriateCompanyCode(session, null));
				row.setCategoryItemLine(((ItemLinePk)session.getAttribute(ProjectConstants.SESSION_EDIT_ITEM_LINE_PK)).getItemLine());				
				row.setDcInd(GENERIC_NO);
				row.setCategoryItemCategory(String.valueOf(-1*(rows.size() - 1 )));
			}
		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}
	}

	protected void beforeDeleteRow(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		HttpSession session = request.getSession(false);
		ItemCategoryForm form = (ItemCategoryForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {

		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}
	}

	protected void afterDeleteRow(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		HttpSession session = request.getSession(false);
		ItemCategoryForm form = (ItemCategoryForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {

		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}
	}

	protected String getSimpleSrchMenuId() {

		//TODO: Add the following line to ProjectMenuConstants.java
		//public static final String MOD_ITEM_CATEGORY = "MOD_ITEM_CATEGORY";
		return ProjectConstants.MOD_ITEM_CATEGORY;
	}

	////////////////////////////////////////////////////////////////////////////////
	// END OF OPTIONAL METHODS
	////////////////////////////////////////////////////////////////////////////////


	////////////////////////////////////////////////////////////////////////////////
	// IMPLEMENTS PARENT'S ABSTRACT METHODS
	////////////////////////////////////////////////////////////////////////////////

	protected String getModuleName(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request,HttpServletResponse response) {

		//TODO: Add the following line to ProjectMenuConstants.java
		//public static final String MOD_ITEM_CATEGORY = "MOD_ITEM_CATEGORY";
		return ProjectConstants.MOD_ITEM_CATEGORY;
	}

	protected String getFunctionId() {

		//struts-config xxxxx.do
		return "itemCategory.do";
	}

	protected void setFormType(ActionMapping mapping, ActionForm formIn, HttpServletRequest request,
		 HttpServletResponse response, ActionErrors errors) {

		ItemCategoryForm form = (ItemCategoryForm) formIn;
		form.setFormType(ProjectConstants.FORM_TYPE_PAGE_EDIT);
	}

	protected String getMasterDelegateName() {

		//the corresponding delegate session name
		return "itemCategoryDelegate";
	}

	protected Class getMasterFormClass() {

		//the corresponding form class
		return ItemCategoryForm.class;
	}

	protected Class getMasterVOClass() {

		//corresponding master vo class
		return ItemCategoryVo.class;
	}

	protected Class getDetailVOClass() {

		//corresponding detail vo class
		return ItemCategoryVo.class;
	}

	protected Class getDetailFormClass() {
		//corresponding detail row class
		return ItemCategoryRow.class;
	}

	protected String getDetailFormSessionName() {

		//corresponding detail form session name
		return "itemCategoryForm";
	}

	protected Class getSearchCriteriaClass() {

		//corresponding searching criteria class
		return ItemCategorySrchCriteria.class;
	}

	protected void setOtherCriteria(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response,
		ActionErrors errors, Object criteria) {

		HttpSession session = request.getSession(false);
		User user = (User) session.getAttribute(ProjectConstants.SESSION_USER);
		ItemCategorySrchCriteria crit = (ItemCategorySrchCriteria) criteria;
		ItemCategoryForm form = (ItemCategoryForm) formIn;

		ItemLinePk itemLinePk = (ItemLinePk) session.getAttribute(ProjectConstants.SESSION_EDIT_ITEM_LINE_PK);
		crit.setSrchCompanyCode(itemLinePk.getCompanyCode());
		crit.setSrchItemLineFr(itemLinePk.getItemLine());
		
		form.setSrchItemLineFr(itemLinePk.getItemLine());
		
		
		crit.setSearchResult(true);
	}

	////////////////////////////////////////////////////////////////////////////////
	// END OF ABSTRACT METHODS
	////////////////////////////////////////////////////////////////////////////////
}
